<?php

/**
 * Application Model Mappers
 *
 * @package Application_Model
 * @subpackage Mapper
 * @author <YOUR NAME HERE>
 * @copyright ZF model generator
 * @license http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * Data Mapper implementation for Application_Model_Formation
 *
 * @package Application_Model
 * @subpackage Mapper
 * @author <YOUR NAME HERE>
 */
class Application_Model_Mapper_Formation extends Application_Model_Mapper_MapperAbstract
{
    /**
     * Returns an array, keys are the field names.
     *
     * @param Application_Model_Formation $model
     * @return array
     */
    public function toArray($model)
    {
        if (! $model instanceof Application_Model_Formation) {
            throw new Exception('Unable to create array: invalid model passed to mapper');
        }

        $result = array(
            'id_for' => $model->getIdFor(),
            'modele_for' => $model->getModeleFor(),
            'libelle_for' => $model->getLibelleFor(),
            'nbrheur_for' => $model->getNbrheurFor(),
            'nbrjour_for' => $model->getNbrjourFor(),
            'description_for' => $model->getDescriptionFor(),
            'enseignant_cin_enseig' => $model->getEnseignantCinEnseig(),
        );

        return $result;
    }

    /**
     * Returns the DbTable class associated with this mapper
     *
     * @return Application_Model_DbTable_Formation
     */
    public function getDbTable()
    {
        if ($this->_dbTable === null) {
            $this->setDbTable('Application_Model_DbTable_Formation');
        }

        return $this->_dbTable;
    }

    /**
     * Deletes the current model
     *
     * @param Application_Model_Formation $model The model to delete
     * @see Application_Model_DbTable_TableAbstract::delete()
     * @return int
     */
    public function delete($model)
    {
        if (! $model instanceof Application_Model_Formation) {
            throw new Exception('Unable to delete: invalid model passed to mapper');
        }

        $this->getDbTable()->getAdapter()->beginTransaction();
        try {
            $where = $this->getDbTable()->getAdapter()->quoteInto('id_for = ?', $model->getIdFor());
            $result = $this->getDbTable()->delete($where);

            $this->getDbTable()->getAdapter()->commit();
        } catch (Exception $e) {
            $this->getDbTable()->getAdapter()->rollback();
            $result = false;
        }

        return $result;
    }

    /**
     * Saves current row, and optionally dependent rows
     *
     * @param Application_Model_Formation $model
     * @param boolean $ignoreEmptyValues Should empty values saved
     * @param boolean $recursive Should the object graph be walked for all related elements
     * @param boolean $useTransaction Flag to indicate if save should be done inside a database transaction
     * @return boolean If the save action was successful
     */
    public function save(Application_Model_Formation $model,
        $ignoreEmptyValues = true, $recursive = false, $useTransaction = true
    ) {
        $data = $model->toArray();
        if ($ignoreEmptyValues) {
            foreach ($data as $key => $value) {
                if ($value === null or $value === '') {
                    unset($data[$key]);
                }
            }
        }

        $primary_key = $model->getIdFor();
        $success = true;

        if ($useTransaction) {
            $this->getDbTable()->getAdapter()->beginTransaction();
        }

        unset($data['id_for']);

        try {
            if ($primary_key === null) {
                $primary_key = $this->getDbTable()->insert($data);
                if ($primary_key) {
                    $model->setIdFor($primary_key);
                } else {
                    $success = false;
                }
            } else {
                $this->getDbTable()
                     ->update($data,
                              array(
                                 'id_for = ?' => $primary_key
                              )
                );
            }

            if ($recursive) {
                if ($success && $model->getOrdre(false) !== null) {
                    $Ordre = $model->getOrdre();
                    foreach ($Ordre as $value) {
                        $success = $success &&
                            $value->setFormationIdFor($primary_key)
                                  ->save($ignoreEmptyValues, $recursive, false);

                        if (! $success) {
                            break;
                        }
                    }
                }

                if ($success && $model->getQuestionnaire(false) !== null) {
                    $Questionnaire = $model->getQuestionnaire();
                    foreach ($Questionnaire as $value) {
                        $success = $success &&
                            $value->setFormationIdFor($primary_key)
                                  ->save($ignoreEmptyValues, $recursive, false);

                        if (! $success) {
                            break;
                        }
                    }
                }

                if ($success && $model->getSessionformation(false) !== null) {
                    $Sessionformation = $model->getSessionformation();
                    foreach ($Sessionformation as $value) {
                        $success = $success &&
                            $value->setFormationIdFor($primary_key)
                                  ->save($ignoreEmptyValues, $recursive, false);

                        if (! $success) {
                            break;
                        }
                    }
                }

            }

            if ($useTransaction && $success) {
                $this->getDbTable()->getAdapter()->commit();
            } elseif ($useTransaction) {
                $this->getDbTable()->getAdapter()->rollback();
            }

        } catch (Exception $e) {
            if ($useTransaction) {
                $this->getDbTable()->getAdapter()->rollback();
            }

            $success = false;
        }

        return $success;
    }

    /**
     * Finds row by primary key
     *
     * @param int $primary_key
     * @param Application_Model_Formation|null $model
     * @return Application_Model_Formation|null The object provided or null if not found
     */
    public function find($primary_key, $model)
    {
        $result = $this->getRowset($primary_key);

        if (is_null($result)) {
            return null;
        }

        $row = $result->current();

        $model = $this->loadModel($row, $model);

        return $model;
    }

    /**
     * Loads the model specific data into the model object
     *
     * @param Zend_Db_Table_Row_Abstract|array $data The data as returned from a Zend_Db query
     * @param Application_Model_Formation|null $entry The object to load the data into, or null to have one created
     * @return Application_Model_Formation The model with the data provided
     */
    public function loadModel($data, $entry)
    {
        if ($entry === null) {
            $entry = new Application_Model_Formation();
        }

        if (is_array($data)) {
            $entry->setIdFor($data['id_for'])
                  ->setModeleFor($data['modele_for'])
                  ->setLibelleFor($data['libelle_for'])
                  ->setNbrheurFor($data['nbrheur_for'])
                  ->setNbrjourFor($data['nbrjour_for'])
                  ->setDescriptionFor($data['description_for'])
                  ->setEnseignantCinEnseig($data['enseignant_cin_enseig']);
        } elseif ($data instanceof Zend_Db_Table_Row_Abstract || $data instanceof stdClass) {
            $entry->setIdFor($data->id_for)
                  ->setModeleFor($data->modele_for)
                  ->setLibelleFor($data->libelle_for)
                  ->setNbrheurFor($data->nbrheur_for)
                  ->setNbrjourFor($data->nbrjour_for)
                  ->setDescriptionFor($data->description_for)
                  ->setEnseignantCinEnseig($data->enseignant_cin_enseig);
        }

        $entry->setMapper($this);

        return $entry;
    }
}
